﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Globalization;

namespace JasRibbonApplicatie.Classes
{
    class EtikettenPagina
    {

        public string PapierSoort { get; set; }

        //pagina
        public Thickness PageMargin { get; set; }
        public int Rijen { get; set; }
        public int Kolommen { get; set; }
        public int PageWith { get; set; }
        public int PageHeight { get; set; }

        //label
        public int LabelBreedte { get; set; }
        public int LabelHoogte { get; set; }
        public Thickness LabelMargin { get; set; }

        public EtikettenPagina(string papierSoort, Thickness pageMargin, int kolommen, int rijen, int labelBreedte, int labelHoogte, Thickness labelMargin)
        {
            this.PapierSoort = papierSoort;
            this.PageMargin = pageMargin;
            this.Rijen = rijen;
            this.Kolommen = kolommen;
            this.LabelBreedte = labelBreedte;
            this.LabelHoogte = labelHoogte;
            this.LabelMargin = labelMargin;

            //standaard A4
            PageHeight = LabelHoogte * Rijen;//(LabelHoogte+int.Parse(labelMargin.Bottom.ToString()) + int.Parse(labelMargin.Top.ToString())) * Rijen + int.Parse((pageMargin.Top + pageMargin.Bottom).ToString());
            PageWith = LabelBreedte * Kolommen;


        }

        internal Grid SetPreview(System.Windows.Controls.Grid myCanvas, int aantalPagina, StreepjesCode.CodeType codeType, string startCode)
        {
            int maxId = int.Parse(startCode.Substring(1), NumberStyles.HexNumber);
            int count = myCanvas.Children.Count;
            myCanvas.Children.RemoveRange(0, count);
            Grid mainGrid = new Grid();
            for (int p = 0; p < aantalPagina; p++)
            {
                // Create the Grid
                Grid myGrid = new Grid();
                myGrid.Width = PageWith;
                //myGrid.Height = PageHeight;
                myGrid.Margin = PageMargin; //new Thickness(PageMargin.Left, (PageMargin.Top + PageHeight) * p + PageMargin.Top, PageMargin.Right, PageMargin.Bottom);
                myGrid.HorizontalAlignment = HorizontalAlignment.Left;
                myGrid.VerticalAlignment = VerticalAlignment.Top;
                myGrid.ShowGridLines = false;

                for (int k = 0; k < Kolommen; k++)
                {
                    // Define the Columns
                    ColumnDefinition colDef = new ColumnDefinition();
                    myGrid.ColumnDefinitions.Add(colDef);
                }
                for (int r = 0; r < Rijen; r++)
                {
                    // Define the Rows
                    RowDefinition rowDef = new RowDefinition();
                    myGrid.RowDefinitions.Add(rowDef);
                }

                for (int r = 0; r < Rijen; r++)
                {
                    for (int k = 0; k < Kolommen; k++)
                    {
                        String code = StreepjesCode.ConvertToStreepjesCode(codeType, ++maxId).ToUpper();
                        Image label = new Image();
                        label.Source = (new ImageSourceConverter()).ConvertFromString(StreepjesCode.getStreepjesCode(code)) as ImageSource;
                        label.Margin = LabelMargin;
                        label.Width = LabelBreedte - LabelMargin.Left - LabelMargin.Right;
                        label.Height = LabelHoogte- LabelMargin.Top - LabelMargin.Bottom;
                        label.Stretch = Stretch.Fill;
                        Grid.SetRow(label, r);
                        Grid.SetColumn(label, k);

                        Label textLabel = new Label();
                        textLabel.Content = code;
                        textLabel.HorizontalAlignment = HorizontalAlignment.Center;
                        textLabel.VerticalAlignment = VerticalAlignment.Top;
                        textLabel.Background = Brushes.White;
                        textLabel.Padding = new Thickness(2,0,2,0);
                        textLabel.Margin = new Thickness(0, label.Height+label.Margin.Top-10, 0, 10);
                        Grid.SetRow(textLabel, r);
                        Grid.SetColumn(textLabel, k);
                        
                        myGrid.Children.Add(label);
                        myGrid.Children.Add(textLabel);
                    }
                }
                //mainGrid.Children.Add(myGrid);
                myCanvas.Children.Add(myGrid);
            }
            return mainGrid;
            
        }
    }
}